﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using IPC.Communication.Framework.Reflection;
using IPC.Communication.Framework.Core;


namespace IPC.Communication.Framework.LogNet
{
    public class LogStatistics : LogStatisticsBase<long>
    {
        private RegularByteTransform byteTransform;

        private long totalSum = 0L;

        [HslMqttApi(HttpMethod = "GET", Description = "Get the sum of all current values")]
        public long TotalSum
        {
            get
            {
                return this.totalSum;
            }
        }

        public LogStatistics(GenerateMode generateMode, int dataCount)
            : base(generateMode, dataCount)
        {
            this.byteTransform = new RegularByteTransform();
        }

        [HslMqttApi(Description = "Adding a new statistical information will determine the position to insert the data according to the current time.")]
        public void StatisticsAdd(long frequency = 1L)
        {
            Interlocked.Add(ref this.totalSum, frequency);
            base.StatisticsCustomAction((long m) => m + frequency);
        }

        [HslMqttApi(Description = "Adding a new statistical information will determine the position to insert the data according to the specified time.")]
        public void StatisticsAddByTime(long frequency, DateTime time)
        {
            Interlocked.Add(ref this.totalSum, frequency);
            base.StatisticsCustomAction((long m) => m + frequency, time);
        }

        public byte[] SaveToBinary()
        {
            OperateResult<long, long[]> statisticsSnapAndDataMark = base.GetStatisticsSnapAndDataMark();
            int num = 1024;
            byte[] array = new byte[statisticsSnapAndDataMark.Content2.Length * 8 + num];
            BitConverter.GetBytes(305419896).CopyTo(array, 0);
            BitConverter.GetBytes((ushort)num).CopyTo(array, 4);
            BitConverter.GetBytes((ushort)base.GenerateMode).CopyTo(array, 6);
            BitConverter.GetBytes(statisticsSnapAndDataMark.Content2.Length).CopyTo(array, 8);
            BitConverter.GetBytes(statisticsSnapAndDataMark.Content1).CopyTo(array, 12);
            BitConverter.GetBytes(this.TotalSum).CopyTo(array, 20);
            for (int i = 0; i < statisticsSnapAndDataMark.Content2.Length; i++)
            {
                BitConverter.GetBytes(statisticsSnapAndDataMark.Content2[i]).CopyTo(array, num + i * 8);
            }
            return array;
        }

        public void SaveToFile(string fileName)
        {
            File.WriteAllBytes(fileName, this.SaveToBinary());
        }

        public void LoadFromBinary(byte[] buffer)
        {
            int num = BitConverter.ToInt32(buffer, 0);
            bool flag = num != 305419896;
            if (flag)
            {
                throw new Exception("File is not LogStatistics file, can't load data.");
            }
            int index = (int)BitConverter.ToUInt16(buffer, 4);
            GenerateMode generateMode = (GenerateMode)BitConverter.ToUInt16(buffer, 6);
            int length = BitConverter.ToInt32(buffer, 8);
            long lastDataMark = BitConverter.ToInt64(buffer, 12);
            long num2 = BitConverter.ToInt64(buffer, 20);
            this.generateMode = generateMode;
            this.totalSum = num2;
            long[] statistics = this.byteTransform.TransInt64(buffer, index, length);
            base.Reset(statistics, lastDataMark);
        }

        public void LoadFromFile(string fileName)
        {
            bool flag = File.Exists(fileName);
            if (flag)
            {
                this.LoadFromBinary(File.ReadAllBytes(fileName));
            }
        }

        public override string ToString()
        {
            return string.Format("LogStatistics[{0}:{1}]", base.GenerateMode, base.ArrayLength);
        }
    }
}
